<?php

namespace backend\controllers;

use backend\components\PayToUser;
use backend\service\SendWechatMessage;
use Yii;
use backend\models\MallAppear;
use backend\models\MallAppearSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;

/**
 * MallAppearController implements the CRUD actions for MallAppear model.
 */
class MallAppearController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['POST'],
                ],
            ],
        ];
    }

    /**
     * Lists all MallAppear models.
     * @return mixed
     */
    public function actionIndex()
    {

        $searchModel = new MallAppearSearch();
        $dataProvider = $searchModel->search(Yii::$app->request->queryParams);

        return $this->render('index', [
            'searchModel' => $searchModel,
            'dataProvider' => $dataProvider,
        ]);
    }

    /**
     * Displays a single MallAppear model.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionView($id)
    {
        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new MallAppear model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new MallAppear();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

    /**
     * Updates an existing MallAppear model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        }

        return $this->render('update', [
            'model' => $model,
        ]);
    }

    /**
     * Deletes an existing MallAppear model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     * @throws NotFoundHttpException if the model cannot be found
     */
    public function actionDelete($id)
    {
        //$this->findModel($id)->delete();

        return $this->redirect(['index']);
    }

    /**
     * Finds the MallAppear model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return MallAppear the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = MallAppear::findOne($id)) !== null) {
            return $model;
        }

        throw new NotFoundHttpException('The requested page does not exist.');
    }

    /**
     * 去掉此功能
     * @param $id
     * @return \yii\web\Response
     */
    public function actionAmount($id)
    {
        /**
        $appear = $this->findModel($id);

        $pay = new PayToUser();
        $result = $pay->send($appear->openid, $appear->amount);

        if ($result['return_code'] == "SUCCESS") {
            if ($result['return_msg'] == "支付失败") {
                Yii::$app->session->setFlash('yes', "message");
                Yii::$app->session->setFlash('message', $result['err_code_des']);
                return $this->redirect(['index']);
            }
            //提现成功
            //减去用户教师金额
            //给用户发送模板消息
            $redis = Yii::$app->redis;
            $SendWeChatMessage = new SendWechatMessage();
            while (true) {
                $formid = $redis->rpop('formid_' . $appear->openid);
                $status = $SendWeChatMessage->income($appear->openid, $formid, $appear->amount, '提现成功', '请注意查收');
                if ($status['errcode'] == 0 || $formid == false) {
                    break;
                }
            }
            //将此条记录修改为已发放状态以及将返回信息放入
            $appear->status = 2;
            $appear->finish_time = time();
            $appear->check_id = Yii::$app->user->identity->id;
            $appear->save();
        } else {
            //TODO
            //提现失败
            //将金额还给用户现金里
            //发送失败的模板消息
            $redis = Yii::$app->redis;
            $SendWeChatMessage = new SendWechatMessage();
            while (true) {
                $formid = $redis->rpop('formid_' . $appear->openid);
                $status = $SendWeChatMessage->income($appear->openid, $formid, $appear->amount, '提现失败', '请联系客服');
                if ($status['errcode'] == 0 || $formid == false) {
                    break;
                }
            }
            //修改记录为失败并记录失败原因
            $appear->status = 3;
            $appear->finish_time = time();
            $appear->reason = '请联系客服了解情况';
            $appear->check_id = Yii::$app->user->identity->id;
            $appear->save();
        }
         * */

        return $this->redirect(['index']);
    }
}
